﻿using Sttplay.Transmission.SerialPortEx;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace transmission
{
    /// <summary>
    /// SerialPort.xaml 的交互逻辑
    /// </summary>
    public partial class SerialPortCreate : Window
    {
        public SerialPortCreate()
        {
            InitializeComponent();
            this.ResizeMode = ResizeMode.NoResize;
            Left = MainWindow.Instance.Left + MainWindow.Instance.Width / 2 - Width / 2;
            Top = MainWindow.Instance.Top + MainWindow.Instance.Height / 2 - Height / 2;
            string[] coms = SerialPortEx.SerialPorts;
            for (int i = 0; i < coms.Length; i++)
                serialPortName.Items.Add(coms[i]);
            serialPortName.SelectedIndex = 0;
            buardrateText.Text = string.Format("{0}", 9600);

            parityCombox.Items.Add(Parity.None);
            parityCombox.Items.Add(Parity.Odd);
            parityCombox.Items.Add(Parity.Even);
            parityCombox.Items.Add(Parity.Mark);
            parityCombox.Items.Add(Parity.Space);
            parityCombox.SelectedIndex = 0;

            dataBitsCombox.Items.Add(5);
            dataBitsCombox.Items.Add(6);
            dataBitsCombox.Items.Add(7);
            dataBitsCombox.Items.Add(8);
            dataBitsCombox.SelectedIndex = 3;

            stopBitsCombox.Items.Add(StopBits.None);
            stopBitsCombox.Items.Add(StopBits.One);
            stopBitsCombox.Items.Add(StopBits.OnePointFive);
            stopBitsCombox.Items.Add(StopBits.Two);
            stopBitsCombox.SelectedIndex = 1;
        }

        public string GetCOM()
        {
            if (serialPortName.Items.Count <= 0)
                return null;
            return serialPortName.SelectedItem as string;
        }

        public int GetBuardrate()
        {
            return int.Parse(buardrateText.Text.Trim());
        }

        public Parity GetParity()
        {
            return (Parity)parityCombox.SelectedItem;
        }

        public int GetDataBits()
        {
            return (int)dataBitsCombox.SelectedItem;
        }

        public StopBits GetStopBits()
        {
            return (StopBits)stopBitsCombox.SelectedItem;
        }

        private void OnBuardRateInputText(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !Regex.IsMatch(e.Text, @"^[1-9]\d*|0$");
        }

        private void OnSureClicked(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(GetCOM()))
            {
                MessageBox.Show("COM 端口无效", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            if (!Tools.ValidatePort(buardrateText.Text))
            {
                MessageBox.Show("Port格式错误", "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            DialogResult = true;
        }

        private void OnMouseEnter(object sender, MouseEventArgs e)
        {
            Console.WriteLine("enter");
        }

    }
}
